<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">DSE课程管理系统</h2>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="80px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login-btn" @click="handleLogin" :loading="loading">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
const useStore = useUserStore()

interface LoginForm {
  username: string
  password: string
  remember: boolean
}

const router = useRouter()
const loading = ref(false)
const loginFormRef = ref<FormInstance>()

const loginForm = reactive<LoginForm>({
  username: '',
  password: '',
  remember: false,
})

// 表单验证规则
const loginRules = reactive<FormRules<LoginForm>>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
  ],
})

const loadRememberedCredentials = () => {
  const remembered = localStorage.getItem('rememberedCredentials')
  if (remembered) {
    const credentials = JSON.parse(remembered)
    loginForm.username = credentials.username
    loginForm.password = credentials.password
    loginForm.remember = true
  }
}

const saveRememberedCredentials = () => {
  if (loginForm.remember) {
    const credentials = {
      username: loginForm.username,
      password: loginForm.password,
    }
    localStorage.setItem('rememberedCredentials', JSON.stringify(credentials))
  } else {
    localStorage.removeItem('rememberedCredentials')
  }
}

const handleLogin = async () => {
  try {
    await loginFormRef.value?.validate()
    loading.value = true
    await useStore.doLogin(loginForm.username, loginForm.password)
    saveRememberedCredentials()
    ElMessage.success('登录成功')
    router.push('/')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadRememberedCredentials()
})
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.login-card {
  width: 400px;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

.login-form {
  margin-top: 20px;
}

.login-btn {
  width: 100%;
}
</style>
